package me.goldze.mvvmhabit.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import me.goldze.mvvmhabit.R;
import me.goldze.mvvmhabit.utils.StringUtils;

/**
 * @author PACY
 * @date 2021/11/25
 * description: toolbar封装
 */
public class BaseToolbar extends RelativeLayout {
    private String title;
    private boolean showBackIcon;
    private boolean showLine;
    private String menu1Text;

    BackClick backClick;

    MenuClick menuClick;

    public TextView titleTv;
    public ImageView leftIcon;
    public TextView menu1_tv;
    public TextView line;

    public RelativeLayout toolbar;

    //主题色
    public static final int BASE_THEME = 1;//白色
    public static final int OTHER_THEME = 2;//其他色

    public int TYPE_THEME = BASE_THEME;//当前主题 默认白色

    public BaseToolbar(Context context) {
        super(context);
    }

    public BaseToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.base_toolbar_view, this, true);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BaseToolbar);
        title = a.getString(R.styleable.BaseToolbar_title);
        showBackIcon = a.getBoolean(R.styleable.BaseToolbar_showBackIcon, true);
        showLine = a.getBoolean(R.styleable.BaseToolbar_showLine, true);
        menu1Text = a.getString(R.styleable.BaseToolbar_menu1);
        a.recycle();
    }

    public void setBackClick(BackClick backClick) {
        this.backClick = backClick;
    }

    public void setMenuClick(MenuClick menuClick) {
        this.menuClick = menuClick;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        titleTv = findViewById(R.id.base_title);
        leftIcon = findViewById(R.id.base_left_icon);
        menu1_tv = findViewById(R.id.menu1_tv);
        toolbar = findViewById(R.id.toolbar);
        line = findViewById(R.id.line);
        if (showBackIcon) {
            leftIcon.setVisibility(VISIBLE);
        } else {
            leftIcon.setVisibility(GONE);
        }
        line.setVisibility(showLine ? VISIBLE : GONE);

        if (StringUtils.isEmpty(menu1Text)) {
            setTitle(title);
        } else {
            setTitleAndMenu1(title, menu1Text);
        }
        setThemeType(BASE_THEME);
    }


    public void setTitleAndMenu1(String title, String menu1) {
        menu1_tv.setVisibility(VISIBLE);
        menu1_tv.setText(menu1);
        setTitle(title);

        menu1_tv.setOnClickListener(v -> {
            if (menuClick != null) {
                menuClick.menu1Click();
            }

        });
    }

    /**
     * 设置图标
     *
     * @param iconResource
     */
    public void setLeftIcon(int iconResource) {
        leftIcon.setBackgroundResource(iconResource);
        leftIcon.setVisibility(VISIBLE);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleTv.setText(title);
        leftIcon.setOnClickListener((v) -> {
            if (backClick != null) {
                backClick.onBack();
            }

        });
    }

    /**
     * 设置当前主题色
     *
     * @param type
     */
    public void setThemeType(int type) {
        TYPE_THEME = type;
        switch (TYPE_THEME) {
            case BASE_THEME://白色
                titleTv.setTextColor(ContextCompat.getColor(getContext(), R.color.black));
                toolbar.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.white100));
                leftIcon.setImageResource(R.drawable.icon_back);
                menu1_tv.setTextColor(ContextCompat.getColor(getContext(), R.color.black));
                break;

            case OTHER_THEME://蓝色

                titleTv.setTextColor(ContextCompat.getColor(getContext(), R.color.white100));
                toolbar.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
                leftIcon.setImageResource(R.drawable.back_w2);
                menu1_tv.setTextColor(ContextCompat.getColor(getContext(), R.color.white100));
                break;
        }
    }

}
